package com.cxl.price.top.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.cxl.price.util.SummerGlobals;
import com.summer.core.container.SessionContainerUtil;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.TaobaoRequest;
import com.taobao.api.TaobaoResponse;
import com.taobao.api.domain.User;

@Service
public class TopBaseService  extends SessionContainerUtil {
	
	public  String token;
	public  User user;
	public 	HttpServletRequest req;
	
	public static Map<Object,TaobaoRequest> map;
	
	/**
	 * 获得接入上家平台接入信息
	 * @param url
	 * @param appKey
	 * @param MYSECRET
	 * @return
	 */
		final String type=SummerGlobals.getProperty("top.type");
		final String url=SummerGlobals.getProperty("top."+type+".url");
		final String appkey=SummerGlobals.getProperty("top."+type+".appkey");
		final String secret=SummerGlobals.getProperty("top."+type+".secret");
		final String tokenUrl=SummerGlobals.getProperty("top."+type+".tokenUrl");
		final String oauth=SummerGlobals.getProperty("top."+type+".oauth");
		
	
	public TaobaoClient getTaobaoClient(){
		 TaobaoClient client=new DefaultTaobaoClient(url,appkey,secret);
		 return client;
	}
	
	
	public <S extends TaobaoResponse,T extends TaobaoRequest<S>> S getTaobaoResponse(T tbReqObj,HttpServletRequest request) throws Exception{
		tbReqObj=(T) getNewTaobaoRequest(tbReqObj, getDefaultRequest(tbReqObj.getClass(),request));//合并
		return  getTaobaoClient().execute(tbReqObj,SessionContainerUtil.getSessionContainer(request).getSessionid());
	}
	
	
	public <S extends TaobaoResponse,T extends TaobaoRequest<S>> S getTaobaoResponse(T tbReqObj)  throws ApiException{
		tbReqObj=(T) getNewTaobaoRequest(tbReqObj, getDefaultRequest(tbReqObj.getClass()));//合并
		return  getTaobaoClient().execute(tbReqObj);
	}
	
	
	
	public  <S extends TaobaoResponse,T extends TaobaoRequest<S>> S getTaobaoResponse(Class<T> clazz,HttpServletRequest request) throws Exception{
		String access_token=SessionContainerUtil.getSessionContainer(request).getSessionid();
		T t=getDefaultRequest(clazz,request);
		return getTaobaoClient().execute(t,access_token);
	}
	
	public  <S extends TaobaoResponse,T extends TaobaoRequest<S>> S getTaobaoResponse(Class<T> clazz) throws ApiException{
		T t=getDefaultRequest(clazz);
		return getTaobaoClient().execute(t);
	}
	
	
	public  <S extends TaobaoResponse,T extends TaobaoRequest<S>> T  getDefaultRequest(Class<T> clazz,HttpServletRequest request) throws Exception{
		T def=(T) map.get(clazz);
		List<String> dynamicAttrs=(List)map.get(clazz.getName()+"_dynamicAttr");
		if(dynamicAttrs!=null){
			for(String dynamic:dynamicAttrs){
				  Method setMethod=def.getClass().getMethod(dynamic,String.class);
				  if(dynamic.equals("setNick")){
					  setMethod.invoke(def, getSessionContainer(request).getUser().getNick());
				  }
			}
		}
		
		
		return  (T) map.get(clazz);
	}
	
	
	public  <S extends TaobaoResponse,T extends TaobaoRequest<S>> T  getDefaultRequest(Class<T> clazz){
		T def=(T) map.get(clazz);
		return def;
	}
	
	
	
	
	/**
	 * 反射合并2个带查询参数的TaobaoRequest
	 * @param <T>
	 * @param req
	 * @param def
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 */
	public <T extends TaobaoRequest> T getNewTaobaoRequest(T req,T def) 
	{
		try {
		if(req!=null&&def!=null)
		{
			Method[] reqMothods=req.getClass().getMethods();
			Method[] defMothods=def.getClass().getMethods();
			for(Method method:reqMothods)
			{
				for(Method method2:defMothods)
				{
					if(method2.getName()==method.getName()&& method.getName().startsWith("get"))
					{
						
						Object obj1=method.invoke(req,null);
						Object obj2=method2.invoke(def,null);
						if(obj1==null&&obj2!=null)
						{
							String methodName=method.getName().replace("get","set");
							Method setMethod=req.getClass().getMethod(methodName,obj2.getClass());
							setMethod.invoke(req, obj2);
						}
					}
				}
			}
		}else if(req!=null&&def==null){
			return req;
		}else if(req==null&&def!=null){
			return def;
		}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return req;
	}


	public String getToken() {
		return SessionContainerUtil.getSessionContainer(req).getSessionid();
	}


	public void setToken(String token) {
		this.token = token;
	}


	public User getUser() {
		return SessionContainerUtil.getUser(req);
	}


	public void setUser(User user) {
		this.user = user;
	}


	public void setServletRequest(HttpServletRequest arg0) {
		this.req=arg0;//=ServletActionContext.getRequest();
		
	}

	public void setReq(HttpServletRequest req) {
		this.req = req;
	}

	
	
	
//	public S getTaobaoResponse(Class<T> className,Map map) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ApiException
//	{
//		T tbReqObj =className.newInstance();
//		Method[] methods = className.getMethods();
//		   Set<String> key = map.keySet();
//	        for (Iterator it = key.iterator(); it.hasNext();) {
//			String methodName=(String) it.next();
//			Object obj=map.get(methodName);
//			for(Method method :methods)
//			{
//				if(method.getName().equals(methodName))
//				{
//					method.invoke(tbReqObj, obj);
//				}
//			}
//		}
//		S response = getTaobaoClient().execute(tbReqObj);
//		return response;
//	}

	
	
	
}
